package com.demodis.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.demodis.dao.GroupDaoI;
import com.demodis.domain.TblGroup;
import com.demodis.util.DBUtil;

public class GroupDaoImpl extends BaseDao implements GroupDaoI {

	@Override
	public List<TblGroup> getGroup(String flag) {
		String sql = "select a.*, b.userrealName from tbl_group a left join tbl_user b on a.groupLeaderId = b.userId order by showSort asc;";
		List<TblGroup> list = new ArrayList<TblGroup>();
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				TblGroup group = new TblGroup();
				group.setGroupId(rs.getInt("groupId"));
				group.setGroupName(rs.getString("groupName"));
				group.setGroupLeaderId(rs.getInt("groupLeaderId"));
				group.setLeaderName(rs.getString("userRealName"));
				group.setShowSort(rs.getInt("showSort"));
				group.setScore(rs.getDouble("score"));
				list.add(group);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	@Override
	public boolean save(TblGroup group) {
		String sql = "INSERT INTO TBL_GROUP(GROUPNAME,groupLeaderId,showSort) VALUES(?,?,?);";
		conn = DBUtil.getConnection();
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, group.getGroupName());
			ps.setInt(2, group.getGroupLeaderId() == null ? 0 : group.getGroupLeaderId());
			ps.setInt(3, group.getShowSort());
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public boolean modify(TblGroup group) {
		String sql = "UPDATE TBL_GROUP SET GROUPNAME = ?, groupLeaderId = ? ,showSort=? WHERE GROUPID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);

			ps.setString(1, group.getGroupName());
			ps.setInt(2, group.getGroupLeaderId() == null ? 0 : group.getGroupLeaderId());
			ps.setInt(3, group.getShowSort());
			ps.setInt(4, group.getGroupId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public boolean delete(TblGroup group) {
		String sql = "DELETE FROM TBL_GROUP WHERE GROUPID IN(" + group.getIds() + ");";
		int i = 0;
		try {
			conn = DBUtil.getConnection();

			ps = conn.prepareStatement(sql);
			i = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i > 0 ? true : false;
	}

	@Override
	public List<TblGroup> getFenguan(int userId) {
		String sql = "SELECT * FROM tbl_group WHERE groupLeaderId = ? order by showSort asc;";
		List<TblGroup> list = new ArrayList<TblGroup>();
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			while (rs.next()) {
				TblGroup group = new TblGroup();
				group.setGroupId(rs.getInt("groupId"));
				group.setGroupName(rs.getString("groupName"));
				list.add(group);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	@Override
	public TblGroup getByGroupId(int groupId) {
		String sql = "SELECT * FROM tbl_group WHERE groupId = ?;";
		TblGroup group = null;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, groupId);
			rs = ps.executeQuery();
			if (rs.next()) {
				group = new TblGroup();
				group.setGroupId(rs.getInt("groupId"));
				group.setGroupName(rs.getString("groupName"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return group;
	}

	@Override
	public boolean saveScore(int groupId, double score) {
		String sql = "UPDATE TBL_GROUP SET score = ?WHERE GROUPID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);

			ps.setDouble(1, score);
			ps.setInt(2, groupId);

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

}
